{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d5347488",
   "metadata": {},
   "source": [
    "# 作业：尝试使用百度API图像识别其中一个\n",
    "* 做用户需求分析并设计一个最小可行性应用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a238c35",
   "metadata": {},
   "source": [
    "# 植物识别\n",
    "* 接口描述\n",
    "```\n",
    "该请求用于识别一张图片，即对于输入的一张图片（可正常解码，且长宽比较合适），输出植物识别结果。\n",
    "```\n",
    "* 在线调试\n",
    "```\n",
    "您可以在 API Explorer 中调试该接口，可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。\n",
    "```\n",
    "* 请求说明\n",
    "```\n",
    "请求示例\n",
    "\n",
    "HTTP 方法：POST\n",
    "\n",
    "请求URL： https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb941d64",
   "metadata": {},
   "source": [
    "# 草药识别小程序\n",
    "## 南苑物语：\n",
    "* 利用植物识别技术，提供照片或上传图片就可以识别植物，人们便可以识别出的植物来了解植物的具体名称及具体信息。\n",
    "\n",
    "## 用户痛点及场景分析\n",
    "* 某日，一人去爬山，由于不小心划到手导致手指流血，但身上没有备用止血的药，这时候他可以拍照识别植物，寻找可以止血的草药\n",
    "* 一人去采药，在找草药的过程中一时忘记草药的模样，这时候他可以拍照识别植物，找到想要采的要药\n",
    "\n",
    "## 用户分析\n",
    "### 目标用户\n",
    "* 核心用户\n",
    "```\n",
    "需要采摘草药的用户\n",
    "```\n",
    "* 主要用户\n",
    "```\n",
    "从事草药买卖的用户\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e47225d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.9c266296263fd0cc85247b860b641a9f.315360000.1965026346.282335-25904832', 'expires_in': 2592000, 'session_key': '9mzdXUXJmfHa6zgF7N9/IRSOu6OEDObANMVLugSRvva/AyrcCsokVc+ZuU9KMZcQL7/cyXfnO+/CKTwJVwoavbBS+p3O+A==', 'access_token': '24.f1438c150e06c892bed8f07f004424ce.2592000.1652258346.282335-25904832', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '3f80c4388463a2da3e854a97d02d416b'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'ilIoXKonMCenk3VGQrpzlyLX',\n",
    "    'client_secret':'GUdk32pHkHkV1G7MHKIDEO1NoeFsZwOo'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "89640c09",
   "metadata": {},
   "outputs": [],
   "source": [
    "qiuzhen_API = response.json()['access_token']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b9f9702",
   "metadata": {},
   "source": [
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d98e850e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a4696c81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和url二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和image二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>baike_num</td>\n",
       "      <td>否</td>\n",
       "      <td>integer</td>\n",
       "      <td>-</td>\n",
       "      <td>返回百科信息的结果数，默认不返回</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          参数       是否必选       类型 可选值范围  \\\n",
       "0      image    和url二选一   string     -   \n",
       "1        url  和image二选一   string     -   \n",
       "2  baike_num          否  integer     -   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...  \n",
       "1  图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...  \n",
       "2                                   返回百科信息的结果数，默认不返回  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 图像识别 url ： https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Xk3bcxe21\n",
    "pd.read_html('https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Xk3bcxe21')[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8ed04b67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'name': '乌蔹莓', 'score': 0.7649375, 'baike_info': {}}, {'name': '龙葵', 'score': 0.10705409, 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E9%BE%99%E8%91%B5/13388282', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/21a4462309f79052b8ef61c506f3d7ca7acbd55c', 'description': '龙葵(学名：Solanum nigrum L.)是茄科、茄属一年生草本植物，全草高30-120厘米；茎直立，多分枝；卵形或心型叶子互生，近全缘；夏季开白色小花，4-10朵成聚伞花序；球形浆果，成熟后为黑紫色。浆果和叶子均可食用，但叶子含有大量生物碱，须经煮熟后方可解毒。全株入药，可散瘀消肿，清热解毒。几乎全中国有分布。喜欢在田边生长，荒地及村庄附近。广泛分布在欧、亚、美洲的温带至热带地区。'}}, {'name': '绞股蓝', 'score': 0.020518309, 'baike_info': {}}, {'name': '少花龙葵', 'score': 0.018368816, 'baike_info': {}}, {'name': '白毛乌蔹莓', 'score': 0.01499114, 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%99%BD%E6%AF%9B%E4%B9%8C%E8%94%B9%E8%8E%93/9447420', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/63d0f703918fa0ec82fdf23a249759ee3d6ddbcb', 'description': '白毛乌蔹莓(学名：Cayratia albifolia C.L.Li)是葡萄科乌蔹莓属植物，乌蔹莓为半木质或草质藤本。小枝圆柱形，有纵棱纹，被灰色柔毛。卷须3分枝，相隔2节间断与叶对生。又名乌蔹草、五叶藤、五爪龙、母猪藤。生于旷野、山谷、林下、路旁。它具有清热解毒，活血散瘀，利尿的功能，可以治疗咽喉肿痛、疖肿、痈疽、疔疮、痢疾、尿血、白浊、跌打损伤、毒蛇咬伤等症。'}}], 'log_id': 1513438532915116030}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('longkui.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload={\n",
    "    'access_token':qiuzhen_API,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c0473f01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'name': '马齿苋', 'score': 0.8962996, 'baike_info': {}}, {'name': '阔叶半枝莲', 'score': 0.004810987, 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E9%98%94%E5%8F%B6%E5%8D%8A%E6%9E%9D%E8%8E%B2/18721532', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/267f9e2f070828381f3045bfecd0be014c086e065b4c', 'description': \"阔叶半枝莲(学名：Portulaca oleracea 'granatus')是由马齿苋和大花马齿苋人工杂交而成的马齿苋科，马齿苋属匍匐性草本植物。大部分地区一年生，海南等气候温暖地区表现为多年生，茎呈鲜艳红紫，茎的基部分枝向外延伸，长达45厘米左右，冠幅可达50厘米；叶互生，顶叶对生或三叶轮生，肉质，匙形至倒卵形，绿色。花顶生，每茎着生花蕾数枚，但每天每茎仅能开花一朵，花形单瓣，花色有红、橙红、桃红、黄、白等色，春末至初冬，花开花谢从不间断，花期约为4-11月；雄蕊多枚，花药黄色；雌蕊一枚，先端四裂或五裂，线形，长出雄蕊外；受精后成为蒴果，果实呈半帽状。果内种子多数，细小黑色，扁圆形。原产于南美洲，中国各地均有栽培。阔叶半枝莲喜温暖气候，稍耐低温，不畏炎热，不耐霜冻。阔叶半枝莲属强日照植物，如果日照不足，造成枝叶徒长，开花不良或者不开花。对土壤要求不严，但以富含有机质的沙性土壤最为适宜。阔叶半枝莲具有较高的营养、药用和观赏价值。因其花型美观、栽培简单易活、管理方便等因素，中国浙江广泛栽培于公园、道路绿化带以作观赏用，成为浙江当地夏秋季辅助蜜粉源植物之一，对蜜蜂的繁殖和蜂王浆生产起到了很好的辅助作用。(概述图参考来源：)\"}}, {'name': '大花马齿苋', 'score': 0.004314586, 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%A4%A7%E8%8A%B1%E9%A9%AC%E9%BD%BF%E8%8B%8B/893341', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/72f082025aafa40f4bfb1bde5328144f78f0f736b329', 'description': '大花马齿苋(学名：Portulaca grandiflora Hook.)：一年生草本，高10-30厘米。茎平卧或斜升，紫红色，多分枝，节上丛生毛。叶密集枝端，较下的叶不规则互生，叶片细圆柱形，无毛。花单生或数朵簇生枝端，直径2.5-4厘米，日开夜闭；叶状总苞8-9片，轮生，具白色长柔毛；花瓣5或重瓣，倒卵形，顶端微凹，长12-30毫米，红色、紫色或黄白色。蒴果近椭圆形，盖裂；种子细小，多数，圆肾形，直径不及1毫米。花期6-9月，果期8-11月。原产巴西。中国公园、花圃常有栽培，是一种美丽的花卉，繁殖容易，扦插或播种均可。全草可供药用，有散瘀止痛、清热、解毒消肿功效，用于咽喉肿痛、烫伤、跌打损伤、疮疖肿毒。(概述图参考资料来源：)'}}, {'name': '毛马齿苋', 'score': 0.0024873605, 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%AF%9B%E9%A9%AC%E9%BD%BF%E8%8B%8B/8039059', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/7e3e6709c93d70cfa139bc18fadcd100bba12be7', 'description': '毛马齿苋( Portulaca pilosa)，一年生或多年生草本，高5-20厘米。'}}, {'name': '凹叶景天', 'score': 0.0012824838, 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%87%B9%E5%8F%B6%E6%99%AF%E5%A4%A9/1710587', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/267f9e2f07082838d1b4a19ebb99a9014d08f1fb', 'description': '凹叶景天(拉丁文名：Sedum emarginatum Migo)别名：石板菜、九月寒、打不死、石板还阳、石雀还阳、岩板菜，.景天科景天属多年生草本植物。茎节的下部平卧于地面或地下、节上生有不定根；上部直立，淡紫色，略呈四棱形；叶片顶端圆而且有一个凹陷；枝叶密集如地毯，花较小，黄色，着生在花枝的顶端。花期5-6月，果期6月。室外越冬时部分叶片紫红色。耐旱，喜半阴环境。产云南、四川、湖北、湖南、江西、安徽、浙江、江苏、甘肃、陕西、福建等地。生于海拔600-1800米处山坡阴湿处。全草药用，可清热解毒，散瘀消肿，治跌打损伤、热疖、疮毒等病症。(名片图出自：)'}}], 'log_id': 1513439098743523753}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('machixian.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload={\n",
    "    'access_token':qiuzhen_API,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "418c9f8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "s"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
